package com.eurasia.assignment.service.impl;

import com.eurasia.assignment.domain.Assignment;
import com.eurasia.assignment.infrastructure.repository.AssignmentRepository;
import com.eurasia.assignment.service.AssignmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class AssignmentServiceImpl implements AssignmentService {
    private final AssignmentRepository assignmentRepository;

    @Override
    public Optional<Assignment> findById(Long id) {
        return assignmentRepository.findById(id);
    }

    @Override
    public Assignment addAssignment(Assignment assignment) {
        Assignment assign = new Assignment();
        assign.setTitle(assignment.getTitle());
        assign.setContent(assignment.getContent());
        assign.setCreatedAt(LocalDateTime.now());
        return assignmentRepository.save(assign);
    }

    @Override
    public boolean deleteById(Long id) {
        Optional<Assignment> assignment = assignmentRepository.findById(id);
        if (assignment.isEmpty()) {
            throw new IllegalArgumentException("Assignment not found");
        }
        assignmentRepository.deleteById(id);
        return true;
    }

    @Override
    public Assignment updateAssignment(Long id, Assignment assignment) {
        Optional<Assignment>assignments = assignmentRepository.findById(id);
        if (assignments.isEmpty()) {
            throw new IllegalArgumentException("Assignment not found");
        }
        Assignment assign = assignments.get();
        assign.setTitle(assignment.getTitle());
        assign.setContent(assignment.getContent());
        return assignmentRepository.save(assign);
    }

    @Override
    public List<Assignment> findAll() {
        return assignmentRepository.findAll();
    }
} 